package qt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import qt.domain.User;
import qt.service.UserSer;
import utils.SessionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by charmyyy on 2017/11/30.
 */
@Controller
public class LoginController {
    @Resource(name = "userSerImpl")
    private UserSer userSer;

    @RequestMapping("/login")
    public String login(HttpServletRequest request,String username,String password){

        User user = userSer.getUser(username);
        if(user!=null){
            if(user.getUpwd().equals(password)) {
                SessionUtils.add(request, "user", user);
                return "index";
            }else {
                request.setAttribute("erroInfo", "密码错误");
                return "login";
            }
        }else {
            //用户不存在
            request.setAttribute("erroInfo", "该用户名不存在");
            return "login";
       }
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        //Object user = request.getSession().getAttribute("user");
        SessionUtils.rmv(request,"user");
        return "login";
    }
}
